/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiUI_Form.h
	An easier way to create and destroy controls and group them.

******************************************************************************/

#include "MaxsiEngine.h"

#if 0

BeginMaxsiUINamespace

//MaxsiUI_Form*	InitializeForm(MaxsiUI_Form* Form,HWND hWnd)
//{
//	if ( Form )
//	{
//		Form->InitializeForm( hWnd);
//	}
//
//	return Form;
//}

MaxsiUI_Form*	InitializeForm(MaxsiUI_Form* Form,ME_Window* Parent)
{
	if ( Form )
	{
		Form->InitializeForm( Parent);
	}

	return Form;
}

MaxsiUI_Form*	TerminateForm(MaxsiUI_Form* Form)
{
	if ( Form )
	{
		Form->TerminateForm();
		Form = 0;
	}

	return Form;
}

MaxsiUI_Form*	ReplaceForm(MaxsiUI_Form* Old, MaxsiUI_Form* New)
{
	ME_Window*		Parent		=	Old->Parent;

	Old->TerminateForm();
	delete Old; // BEWARE
	New->InitializeForm(Parent);
	Parent->Form = New;

	// Todo: Is this the correct method of redrawing?
	InvalidateRect(Parent->WindowHWND, NULL, TRUE); 

	return New;
}

MaxsiUI_Form::MaxsiUI_Form( void )
{
	Initialized		=	false;
	Parent			=	0;
}

//bool MaxsiUI_Form::InitializeForm(HWND hWnd)
//{
//	WindowHWND = hWnd;
//	Initialized = true;
//	return true;
//}
bool MaxsiUI_Form::InitializeForm(ME_Window* NewParent)
{
	this->Parent	=	NewParent;
	WindowHWND		=	Parent->WindowHWND;
	Parent->Form	=	this;

	Initialized = true;

	return true;
}
bool MaxsiUI_Form::ReplaceSelfWith(MaxsiUI_Form* New)
{
	ReplaceForm(this, New);

	return true;
}
bool MaxsiUI_Form::ForceRedraw()
{
	return (InvalidateRect(WindowHWND, NULL, TRUE)>0);
}

			
bool MaxsiUI_Form::TerminateForm()
{
	if (!Initialized) { return false; }
	return false;
}

bool MaxsiUI_Form::RenderForm(HWND hWnd)
{
	if (!Initialized) { return false; }
    PAINTSTRUCT ps          = {0};
	HDC hDC = BeginPaint (hWnd, &ps);

	RECT rectClient;
    GetClientRect (hWnd, &rectClient);

	SetBkMode(hDC,TRANSPARENT);

	HFONT hFont;

	SetTextColor(hDC,RGB(0,0,0));
	RECT TextRect = rectClient;
	TextRect.left+=25; TextRect.top+=25;

	hFont=CreateFont (16, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
	OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
	DEFAULT_PITCH | FF_SWISS, TEXT("Verdana"));
	SelectObject(hDC,hFont);

    DrawText (hDC, TEXT("This should be handled in a child class!\nbool MaxsiUI_Form::RenderForm(HWND hWnd)"), -1, &TextRect, DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);

    EndPaint (hWnd, &ps);

	return true;
}

LRESULT MaxsiUI_Form::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (!Initialized) { return BaseClass::WindowProc(hWnd, message, wParam, lParam); }

	switch (message) 
	{
			case WM_COMMAND:
				break;

			case WM_SIZE:
				NewResolution(LOWORD(lParam),HIWORD(lParam));
				break;

			case WM_PAINT:
				RenderForm(hWnd);
				break;

		default:			
			return (BaseClass::WindowProc(hWnd, message, wParam, lParam));
	}
	return (0);
}


bool MaxsiUI_Form::NewResolution( WORD Width, WORD Height)
{
	InvalidateRect(WindowHWND, NULL, TRUE);
	return true;
}

LINK	int		DrawSplitLine(HDC hDC,size_t yOffset, size_t Width)
{
	for (size_t Y = 1; Y <= 2; Y++)
	{
		for (size_t X = 25; X <= Width-25; X++)
		{
			if (Y!=1)
			{
				SetPixelV(hDC,(int)X,(int)yOffset+(int)Y,RGB(255,255,255));
			}
			else
			{
				SetPixelV(hDC,(int)X,(int)yOffset+(int)Y,RGB(172,168,153));
			}
		}
	}
	return 1;
}

EndMaxsiUINamespace

#endif

